
# coding=utf-8

import os
import time
import random
from utils import MongoDBUtil,mongo_manager
import math
from class_kuwo import Kuwo
MongoDBUtil = MongoDBUtil()

from setting import language_dict,topic_dict,year_dict,style_dict,emotion_dict,scene_dict
def get_proxy():
    port = random.randint(24000, 24400)
    proxies = {'http': f'http://zheng123:zheng123@haproxy.iinti.cn:{port}',
               'https': f'http://zheng123:zheng123@haproxy.iinti.cn:{port}'}
    return proxies

#获取精选歌单
def get_playlist():
    for page in range(1,493): #range(1,493)
        time.sleep(1)
        gets=[]
        play_list= kuwo.get_play_list(page,20)
        try:
            # print(data['data'])
            datas = play_list['data']
            # print(datas[0])
            print(type(datas))
            for data in datas:
                gets.append({'uname':data['uname'],
                             'lossless_mark' : data['lossless_mark'],
                            'favorcnt' : data['favorcnt'],
                            'uid' : data['uid'],
                            'total' : data['total'],
                            'name' : data['name'],
                            'listencnt' : data['listencnt'],
                            'id' : data['id'],
                            'attribute' : data['attribute'],
                            'digest' : data['digest'],
                             'radio_id':data['radio_id'],
                             'desc':data['desc'],
                             'img':data['img'],
                             '_id': data['id']
                             })
            # data2 = data['data']
            # for i in range(len(data2)):
            #     print(page,data2['data'][i])
            insert_data(tablename='playlist',datas=gets)
            print(page,'done')
        except:
            print(page,'failed')
        # print(type(data2['data']))
    return
#获取分类歌单
def get_playlist_tag(key,value,beginpage=None):
    datas =  kuwo.get_play_list_tag(value)
    total = datas['total']
    pages = math.ceil(total/20)
    print(key,'have pages:',pages)
    #临时 网红 ：page:110
    if beginpage is not None:
        beginspages = beginpage
    else:
        beginspages = pages
    for page in range(beginspages,0,-1):
        time.sleep(1)
        gets=[]
        try:
            play_list = kuwo.get_play_list_tag(code=value, pn=page, rn=20)

            if play_list is None:
                print('get data failed')
            datas = play_list['data']
            for data in datas:
                gets.append({'code':value,'tag_name':key,
                             'uname':data['uname'],
                             '_id':str(value)+'_'+data['id'],
                             'lossless_mark' : data['lossless_mark'],
                            'favorcnt' : data['favorcnt'],
                            'uid' : data['uid'],
                            'total' : data['total'],
                            'name' : data['name'],
                            'listencnt' : data['listencnt'],
                            'id' : data['id'],
                            'attribute' : data['attribute'],
                            'digest' : data['digest'],
                             'radio_id':data['radio_id'],
                             'desc':data['desc'],
                             'img':data['img']
                             })

            insert_data(tablename='playlist_tag',datas=gets)
            print(key,page,'done')
        except:
            print(key,page,'failed')
    print(key,'done')
    return

#插入数据
def insert_data(tablename,datas):
    if len(datas) > 0 and datas is not None:
        for data in datas:
            # data['_id'] = data['id']
            try:
                # print(data)
                MongoDBUtil.insert_one(tablename, data)
            except Exception as e:
                print(e)
    else:
        pass


if __name__ == "__main__":
    kuwo = Kuwo()
    # play_list = kuwo.get_play_list(1, 20)
    # print(play_list)
    # print(play_list['total'])
    # play_list_info = kuwo.get_play_list_info(3431253543,1, 20)
    # print(play_list_info)
    # print(type(play_list_info))

    dict_list=[language_dict,topic_dict,year_dict,style_dict,emotion_dict,scene_dict]
    for dict_l in dict_list:
        # 剔除已经跑的部分
        if dict_l in [language_dict,topic_dict,year_dict,style_dict,emotion_dict,scene_dict]:
            print('jump :', dict_l)
            continue
        for key,value in dict_l.items():
            beginpage=None
            # 剔除已经跑的部分
            # if key in ['华语','欧美','韩语','粤语','日语','经典','情歌','电子','纯音乐','伤感','解压','励志','开心','甜蜜','兴奋',
            #     '小语种','网红','70后','80后','90后','00后','翻唱','流行','DJ','古风','佛乐','轻音乐',
            #            'BGM','抖音','游戏','怀旧','合唱','网络','儿童','ACG','影视']:
            #     print('jump :',key)
            #     continue
            get_playlist_tag(key=key,value=value,beginpage=beginpage)
